from selenium import webdriver
import xlrd
import time
from xlutils.copy import copy
from selenium.webdriver.common.by import By


class Test_login:
    book = copy(xlrd.open_workbook(filename="a.xls", encoding_override=True))
    time = time.strftime('%y年%m月%d日%H:%M:%S')

    def test_login_success001(self):  # 正常用户登录
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("lisi")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("123456")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH, '//div [@id="logo"]/a').text
        if element04 == "个人理财系统":
            print("通过")
            self.book.get_sheet(1).write(3, 6, "passed")
            self.book.get_sheet(1).write(3, 7, "晏凯旋")
            self.book.get_sheet(1).write(3, 8, self.time)
            self.book.save("b.xls")

        else:
            driver.save_screenshot("lisi--123456.jpg")
            self.book.get_sheet(1).write(3, 6, "failed")
            self.book.save("b.xls")

        driver.quit()
        assert element04 == "个人理财系统"

    def test_login_success002(self):  # 正常管理员登录
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("admin")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("123456")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH, '//div [@id="logo"]/a').text
        if element04 == "后台管理系统":
            print("通过")
            self.book.get_sheet(1).write(4, 6, "passed")
            self.book.get_sheet(1).write(4, 7, "晏凯旋")
            self.book.get_sheet(1).write(4, 8, self.time)
            self.book.save("b.xls")
        else:
            driver.save_screenshot("admin--123456.jpg")
            self.book.get_sheet(1).write(4, 6, "failed")
            self.book.save("b.xls")

        driver.quit()
        assert element04 == "后台管理系统"

    def test_login_success003(self):  # 特殊密码登录
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("lisi4")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("123456")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH, '//div [@id="logo"]/a').text
        if element04 == "个人理财系统":
            print("通过")
            self.book.get_sheet(1).write(5, 6, "passed")
            self.book.get_sheet(1).write(5, 7, "晏凯旋")
            self.book.get_sheet(1).write(5, 8, self.time)
            self.book.save("b.xls")
        else:
            driver.save_screenshot("admin--123456.jpg")
            self.book.get_sheet(1).write(5, 6, "failed")
            self.book.save("b.xls")

        driver.quit()
        assert element04 == "个人理财系统"

    def test_login_failed(self):  # 普通账号不存在 密码正确
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("lisi44")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("123456")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH, '//div [@class="form-group has-feedback feedback-left '
                                                  'has-error"]/span').text
        if element04 == "用户名不存在":
            print("通过")
            self.book.get_sheet(1).write(7, 6, "passed")
            self.book.get_sheet(1).write(7, 7, "晏凯旋")
            self.book.get_sheet(1).write(7, 8, self.time)
            self.book.save("b.xls")
        else:
            driver.save_screenshot("lisi44--123456.jpg")
            self.book.get_sheet(1).write(7, 6, "failed")
            self.book.save("b.xls")
        driver.quit()
        assert element04 == "用户名不存在"

    def test_login_failed_02(self):  # 普通账号正确 密码错误
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("lisi")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("1234567")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH,
                                        '//div[@class="form-group has-feedback feedback-left has-error"]/span').text
        if element04 == "密码错误":
            print("通过")
            self.book.get_sheet(1).write(8, 6, "passed")
            self.book.get_sheet(1).write(8, 7, "晏凯旋")
            self.book.get_sheet(1).write(8, 8, self.time)
            self.book.save("b.xls")
        else:
            driver.save_screenshot("lisi--1234567.jpg")
            self.book.get_sheet(1).write(8, 6, "failed")
            self.book.save("b.xls")
        driver.quit()
        assert element04 == "密码错误"

    def test_login_failed03(self):  # 管理员账号不存在 密码正确
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("admin3")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("123456")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH, '//div [@class="form-group has-feedback feedback-left '
                                                  'has-error"]/span').text
        if element04 == "用户名不存在":
            print("通过")
            self.book.get_sheet(1).write(9, 6, "passed")
            self.book.get_sheet(1).write(9, 7, "晏凯旋")
            self.book.get_sheet(1).write(9, 8, self.time)
            self.book.save("b.xls")
        else:
            driver.save_screenshot("lisi44--123456.jpg")
            self.book.get_sheet(1).write(9, 6, "failed")
            self.book.save("b.xls")
        driver.quit()
        assert element04 == "用户名不存在"

    def test_login_failed_04(self):  # 管理员账号正确 密码错误
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        element01 = driver.find_element(By.XPATH,
                                        '//input[@type="text" and @placeholder="请输入您的用户名" and @class="form-control"]')
        element01.send_keys("admin")
        element02 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码" ]')
        element02.send_keys("1234567")
        element03 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element03.click()
        element04 = driver.find_element(By.XPATH,
                                        '//div[@class="form-group has-feedback feedback-left has-error"]/span').text
        if element04 == "密码错误":
            print("通过")
            self.book.get_sheet(1).write(10, 6, "passed")
            self.book.get_sheet(1).write(10, 7, "晏凯旋")
            self.book.get_sheet(1).write(10, 8, self.time)
            self.book.save("b.xls")
        else:
            driver.save_screenshot("lisi--1234567.jpg")
            self.book.get_sheet(1).write(10, 6, "failed")
            self.book.save("b.xls")
        driver.quit()
        assert element04 == "密码错误"
